//	CP2013 Traffic Simulator Assignment
//      Traffic Model TileMap
//	Java version
//      Coded by Matthew Johnson


package traffic;

//A map of the directional flow of each square in the model
public class TileMap
{

    //The tiles are stored as a 2D array
    private Direction[][] tiles;
    //The dimensions of the map
    private int width;
    private int height;

    //Constructor: Sets up the Tilemap the the specified size
    TileMap (int w, int h)
    {
        //Copy width and height
        width = w;
        height = h;

        //Create the array
        tiles = new Direction[w][h];

        //Set each tile to undefined
        for (Direction[] d:tiles)
        {
            for (Direction e:d)
            {
                e = Direction.UNDEFINED;
            }
        }
    }

    //Constructor: Initialises a new TileMap the same as another
    TileMap (TileMap other)
    {
        copyFrom(other);
    }

    //Duplicates a different TileMap
    void copyFrom(TileMap other)
    {
        //Copy size
        height = other.height;
        width = other.width;

        //Setup new array
       tiles = new Direction[width][height];

       //Copy the Directions across
       for (int i = 0; i < width; i++)
       {
           for (int j = 0; j < height; j++)
           {
               tiles[i][j] = other.tiles[i][j];
           }
       }
    }

    //Get the Direction at a particular Tile
    public Direction directionAt(Coordinate coord)
    {
	return tiles[coord.x][coord.y];
    }

    //Change a tile's direction
    public void setTile(Coordinate coord, Direction dir)
    {
        tiles[coord.x][coord.y] = dir;
    }

    //Change the direction for a range of tiles
    public void setTileRange(Coordinate start, int w, int h, Direction dir)
    {
    //For each tile in the range
    for (int y = 0; y < h; y++)
	{
            for (int x = 0; x < w; x++)
            {
                //Calculate the tile's coordinate
                Coordinate c = new Coordinate(start);
                c.x += x;
                c.y += y;

                //Set the direction
                setTile(c, dir);
            }
	}
    }

    //Get the Tilemap's width
    public int getWidth(){
        return width;
    }

    //Get the Tilemap's height
    public int getHeight(){
    return height;}

    





}
